<template>
  <view class="container" v-show="hideModal">
    <view class="mask" @touchmove.stop.prevent @click="close()"></view>
    <view class="promotionList" v-if="goodsVal.promotion && orderType === '1'">
      <view class="promotionGoodsList">
        <view class="title flex flex-row " @tap.stop="setGiftMargin">
          <view class="col-9 flex a-item">促销商品列表<view style="padding-left: 50rpx;color: red">{{ goodsVal.promotion.promotionName }}</view></view>
          <view class="col-1 flex a-item" style="font-size: 36rpx"><image class="dropdownImg"  :style="{transform: 'rotate(' + retate + 'deg)'}" style="width: 35rpx;height: 35rpx" src="http://121.41.75.32:8002/file/static/dropdown.png"></image></view>
        </view>
        <view>
          <scroll-view :scroll-top="scrollTop" style="height: 350rpx" scroll-y="true" class="scroll-Y">
          <view v-for="item in promotionGoodsList " style="min-height: 60rpx" :class="{'back':goods.goodsId === item.goodsId && goods.isGift === item.isGift}" class="flex flex-row" @click="setGoods(item)">
              <view class="col-7 flex a-item">{{ item.goodsName }}</view>
              <view class="col-3 flex a-item">{{ `${item.boxNum}箱${item.mainNum}瓶` }}</view>
          </view>
          </scroll-view>
        </view>
      </view>
      <view class="giftList"  :style="{top: giftTop+'rpx'}">
        <view class="title flex flex-row"  @tap.stop="setGiftMargin">
          <div class="fl1">赠品列表</div>
          <div class="fl1" style="color: #c00">可赠数量{{ giveCount || 0}}</div> </view>
        <scroll-view :scroll-top="scrollTop" style="min-height: 350rpx" scroll-y="true" class="scroll-Y">
        <view v-for="item in giftList"  :class="{'back':goods.goodsId === item.goodsId && goods.isGift === item.isGift}" style="height: 60rpx" class="flex flex-row" @click="setGoods(item)">
          <view class="col-7 flex a-item">{{ item.goodsName }}</view>
          <view class="col-3 flex a-item">{{ `${item.boxNum}箱${item.mainNum}瓶` }}</view>
        </view>
        </scroll-view>
      </view>
    </view>
    <view class="content bottom-pos" :style="{height: height+'rpx'}" :animation="animationData" @touchmove.stop.prevent>
      <view class="mainBox" >
        <view class="flex flex-row">
          <view><image :src="oldGoods.imageUrl"></image></view>
            <view>
              <view>{{ goods.goodsName }}</view>
              <view class="flex flex-row" style="width: 550rpx">
                <view class="fl1"   v-if="goods.mobileOrderIsAllowShowTaxPrice !== '0'">
                  <view>￥{{ price.taxPrice}}元/{{ goods.mainUnitName }}</view>
                  <view>￥{{ price.taxBoxPrice }}元/{{ goods.boxUnitName }}</view>
                </view>
                <view class="fl1" >
                  <div style="margin-bottom: 10rpx">
                    商品类型:
                  </div>
                  <picker mode="selector" :disabled="goods.giftId || goods.mobileOrderIsAllowModifyGoodsTag == '0' || goods.promotionType == 3 "  :range="tagList" @change="tagChange" range-key="name">
                    <view class="picker flex j-content a-item">
                      {{ showTagName?showTagName:"请选择类型" }}
                    </view>
                  </picker>
                </view>
              </view>
            </view>
          </view>
        </view>
        <view class="line-division"></view>
        <view class="flex flex-row" >
          <view class="tab col-4">
            <view class="flex j-content a-item wih">数量</view>
            <view class="flex j-content a-item wih">
              <wly-keyboard :ifPromotion="goods.promotionType == 3" @open="addHeight('800')" @submit="submit" decimal="0" @close="height = 600" :value.sync="boxNum" @addPromotionList="addPromotionList"></wly-keyboard>
<!--              <input class="ipt" type="digit"  v-model="boxNum"/>-->
              {{ goods.boxUnitName }}</view>
            <view class="flex j-content a-item wih" v-if="appletExtraDict.mobileOrderIsAllowShowMainUnit === '1'">
              <wly-keyboard :ifPromotion="goods.promotionType == 3" @blur="computedNum" @open="addHeight('900')" decimal="0" @submit="submit" :value.sync="mainNum" @close="height = 600" @addPromotionList="addPromotionList"></wly-keyboard>
<!--              <input class="ipt" type="digit" id="ipt1"  @blur="computedNum" v-model="mainNum"/>-->
              {{ goods.mainUnitName }}</view>
          </view>
          <view class="tab col-3">
            <view class="flex j-content a-item wih">单价</view>
            <view class="flex j-content a-item wih" >
              <span  v-if="goods.isZeroPrice || goods.mobileOrderIsAllowModifyTaxPrice == '0' || goods.giftId || goods.promotionType == 3 " >￥{{price.taxBoxPrice.toFixed(2) }}</span>
              <span v-else class="flex flex-row a-item">
                ￥<wly-keyboard :ifPromotion="goods.isPromotion" @open="addHeight('800')" @submit="submit" :value.sync="price.taxBoxPrice" @close="computedAmount('taxBoxPrice')" :decimal="2"></wly-keyboard>
<!--                ￥<input class="ipt" type="digit" @focus="showKeyboard"  @blur="computedAmount('taxBoxPrice')" v-model="price.taxBoxPrice"/>-->
              </span>
            </view>
            <view class="flex j-content a-item wih" v-if="appletExtraDict.mobileOrderIsAllowShowMainUnit === '1'">
              <span  v-if="goods.isZeroPrice || goods.mobileOrderIsAllowModifyTaxPrice == '0'  || goods.giftId || goods.promotionType == 3 ">￥{{ price.taxPrice.toFixed(2) }}</span>
              <span v-else  class="flex j-content a-item">
                ￥<wly-keyboard :ifPromotion="goods.promotionType == 3 " @open="addHeight('900')" @submit="submit" :value.sync="price.taxPrice" @close="computedAmount('taxPrice')" :decimal="2"></wly-keyboard>
              </span>
<!--              <span v-else class="flex flex-row a-item">￥<input class="ipt" @focus="showKeyboard"   @blur="computedAmount('taxPrice')" v-model="price.taxPrice"/></span>-->
            </view>
          </view>
          <view class="tab col-3">
            <view class="flex j-content a-item wih">小计</view>
            <view class="flex j-content a-item wih">￥{{ sumBoxAmount.toFixed(2) }}</view>
            <view class="flex j-content a-item wih" v-if="appletExtraDict.mobileOrderIsAllowShowMainUnit === '1'">￥{{ sumMainAmount.toFixed(2) }}</view>
          </view>

        </view>
<!--        <view class="flex flex-row j-content-between">-->
<!--&lt;!&ndash;          <view style="color: #527cde;margin-left: 30rpx">查看商品详情</view>&ndash;&gt;-->
<!--          <button @click="onSubmitGoods" type="primary" class="primary_btn btn_margin" size="mini">确定</button>-->
<!--        </view>-->
      </view>
  </view>
</template>

<script>
import {basGoodsTagList} from "@/service/api/shopping";
import WlyKeyboard from "./wly-keyboard/wly-keyboard";
import {giftPromotionDetail, paperGoodsWithEmptyBoxGoodsList} from "../service/api/shopping";

export default{
  name:'writeOff',
  components: {WlyKeyboard},
  props:{
    show:{
      type: Boolean,
      required: true
    },
    goodsVal:{
      type:Object,
    },
    appletExtraDict:{
      type:Object,
      detail:{}
    },
    customerId:{
      type:Number,
    },
    orderType:{
      type:String,
    }
  },
  data() {
    return {
      hideModal: false, //模态框的状态  true-隐藏  false-显示
      animationData: {}, //
      val: 0,
      goodsImgUrl: '',
      price:{
        taxBoxAmount: 0,
        taxAmount: 0,
        taxPrice: 0,
        taxBoxPrice: 0
      },
      giveCount: 0,
      giftTop: 390,
      remark: null,
      goods:{},
      promotionName: null,
      ruleList: null,
      giftList: null,
      promotionGoodsList: null,
      boxNum: 0,
      mainNum: 0,
      tagList: [],
      showTagName: null,
      defaultTag: {},
      isZeroPrice: false,
      oldGoods: {},
      copyGoods: {},
      height:600,
      oldCustomerId: null,
      retate: 0
    }
  },
  created(){
    basGoodsTagList({size:999}).then(res=>{
    }).catch(error=>{
      this.tagList = error.content
      const tag = error.content.find(item=>item.name === '正常商品')
      this.goods.goodsTagId = tag.id
      this.showTagName = tag.name
      this.defaultTag = tag
    })
  },
  watch:{
    show(val) {
      if(val){
        this.showModal()
      }
    },
    customerId(n){
      this.close()
    },
    'boxNum'(n,o){
        console.log('触发1')
        this.goods.boxNum = this.boxNum
        this.goods.taxBoxAmount = Number(n) * Number(this.goods.taxBoxPrice)
        this.goods.goodsCount = Number(this.goods.boxNum) * Number(this.goods.boxUnitCount)+Number(this.goods.mainNum)
        if (this.isZeroPrice || this.goods.giftId){
          this.goods.taxPrice = 0
          this.price.taxPrice = 0
          this.goods.taxBoxPrice = 0
          this.goods.billTaxPrice = 0
          this.goods.billTaxBoxPrice = 0
          this.price.taxBoxPrice = 0
          this.price.taxAmount = 0
          this.price.taxBoxAmount = 0
        }else if (this.goods.isPromotion === 1 && this.goods.promotionType === '1' && !this.goods.isZeroPrice){
          if ((this.goods.goodsCount <= this.goods.cutOffGoodsCount && this.goods.goodsCount >= this.goods.effectGoodsCount) || this.goods.cutOffGoodsCount === 0 || (this.goods.cutOffGoodsCount > 0 && this.goods.goodsCount <= this.goods.cutOffGoodsCount)){

            this.goods.taxPrice = this.goods.promotionTaxPrice
            this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
            this.goods.taxBoxPrice = this.goods.promotionTaxBoxPrice
            this.price.taxBoxPrice = this.goods.newTaxBoxPrice || this.goods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
          }else {
            this.goods.taxPrice =  this.goods.newTaxPrice || this.oldGoods.taxPrice
            this.goods.taxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.taxBoxPrice
            this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
            this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
          }
          this.goods.billTaxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
          this.goods.billTaxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
        }else {
          this.goods.taxPrice =  this.goods.newTaxPrice || this.oldGoods.taxPrice
          this.goods.taxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.taxBoxPrice
          this.goods.billTaxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
          this.goods.billTaxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
          this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
          this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
        }
        // this.price.taxPrice = this.goods.newTaxPrice || this.goods.billTaxPrice || this.goods.taxPrice
        // this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.goods.billTaxBoxPrice || this.goods.taxBoxPrice
        this.goods.taxBoxAmount = this.goods.taxBoxPrice * Number(this.goods.boxNum)
        this.price.taxBoxAmount = this.goods.taxBoxAmount
        this.goods.taxAmount = Number(this.goods.mainNum) * Number(this.price.taxPrice)
        this.price.taxAmount = this.goods.taxAmount
        if(this.goods.giftId){
          if (this.goods.boxNum > this.goods.unGiftBoxCount || this.goods.goodsCount > this.goods.unGiftGoodsCount){
            uni.showModal({title:'提示',content:'活动商品下单数量大于可下单数量'})
            this.goods.boxNum = 0
            this.goods.goodsCount = 0
            this.boxNum = 0
            this.goods.mainNum = 0
            this.mainNum = 0
            this.goodsCount = 0
          }

      }
        if (this.goods.isPromotion){
          this.giveCount = this.getGiftCount(this.goods.isGift == 1)
        }
    },
    'mainNum'(n,o){
      this.goods.mainNum = this.mainNum || 0
      this.goods.taxAmount = Number(n) * Number(this.goods.taxPrice)
      this.goods.goodsCount = Number(this.goods.boxNum) * Number(this.goods.boxUnitCount)+Number(this.goods.mainNum)

      if (this.isZeroPrice || this.goods.giftId){
        this.goods.taxPrice = 0
        this.price.taxPrice = 0
        this.goods.taxBoxPrice = 0
        this.goods.billTaxPrice = 0
        this.goods.billTaxBoxPrice = 0
        this.price.taxBoxPrice = 0
        this.price.taxAmount = 0
        this.price.taxBoxAmount = 0
      }else if (this.goods.isPromotion === 1 && this.goods.promotionType === '1' && !this.goods.isZeroPrice){
        if ((this.goods.goodsCount <= this.goods.cutOffGoodsCount && this.goods.goodsCount >= this.goods.effectGoodsCount) || this.goods.cutOffGoodsCount === 0 || (this.goods.cutOffGoodsCount > 0 && this.goods.goodsCount <= this.goods.cutOffGoodsCount)){
          this.goods.taxPrice = this.goods.promotionTaxPrice
          this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
          this.goods.taxBoxPrice = this.goods.promotionTaxBoxPrice
          this.price.taxBoxPrice = this.goods.newTaxBoxPrice || this.goods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
        }else {
          this.goods.taxPrice =  this.goods.newTaxPrice || this.oldGoods.taxPrice
          this.goods.taxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.taxBoxPrice
          this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
          this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
        }
        this.goods.billTaxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
        this.goods.billTaxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
      }else {
        this.goods.taxPrice =  this.goods.newTaxPrice || this.oldGoods.taxPrice
        this.goods.taxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.taxBoxPrice
        this.goods.billTaxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
        this.goods.billTaxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
        this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
        this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
      }
      // this.price.taxPrice = this.goods.newTaxPrice || this.goods.billTaxPrice || this.goods.taxPrice
      // this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.goods.billTaxBoxPrice || this.goods.taxBoxPrice
      this.goods.taxBoxAmount = this.goods.taxBoxPrice * Number(this.goods.boxNum)
      this.price.taxBoxAmount = this.goods.taxBoxAmount
      this.goods.taxAmount = Number(this.goods.mainNum) * Number(this.price.taxPrice)
      this.price.taxAmount = this.goods.taxAmount
      if(this.goods.giftId){
        if (this.goods.boxNum > this.goods.unGiftBoxCount || this.goods.goodsCount > this.goods.unGiftGoodsCount){
          uni.showModal({title:'提示',content:'活动商品下单数量大于可下单数量'})
          this.goods.boxNum = 0
          this.goods.mainNum = 0
          this.goods.goodsCount = 0
          this.boxNum = 0
          this.mainNum = 0
          this.goodsCount = 0
        }
      }
      if (this.goods.isPromotion){
        this.giveCount = this.getGiftCount(this.goods.isGift == 1)
      }
    },
  },
  computed:{
    sumBoxAmount(){
      return Number(this.goods.boxNum) * Number(this.price.taxBoxPrice)
    },
    sumMainAmount(){
      return Number(this.goods.mainNum) * Number(this.price.taxPrice)
    },
  },
  methods:{
    // 显示遮罩层
    showModal: function() {
      var that = this;
      that.hideModal = true
      var animation = uni.createAnimation({
        duration: 200, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
        timingFunction: 'ease', //动画的效果 默认值是linear
      })
      this.goods = JSON.parse(JSON.stringify(this.goodsVal))
      if (this.orderType === '1'){
        this.promotionName = this.goods.promotionName
        this.promotionId = this.goods.promotionId
        this.promotionCode = this.goods.promotionCode
        if (this.goods.promotion){
          this.getPromotionList(this.goods.promotion.promotionId)
        }
      }
      if (this.goodsVal.goodsId !== this.oldGoods.goodsId || this.goodsVal.goodsTagId !== this.oldGoods.goodsTagId || this.oldCustomerId !== this.customerId ||  this.oldGoods.giftId !== this.goodsVal.giftId){
        this.oldGoods = Object.assign({},this.goods)
        this.oldCustomerId = this.customerId
        let goodsImgUrl = ''
        try {
          goodsImgUrl = JSON.parse(this.goods.imageUrl)[0].path
        }catch (err){
          goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
        }
        this.oldGoods.imageUrl = goodsImgUrl
        this.goods.imageUrl = goodsImgUrl
        this.price.taxPrice = this.goods.giftId?0:this.goods.billTaxPrice || this.goods.taxPrice
        this.price.taxBoxPrice = this.goods.giftId?0:this.goods.billTaxBoxPrice || this.goods.taxBoxPrice
      }else {
        console.log('触发02')

        this.goods = Object.assign({},this.oldGoods)
        this.price.taxPrice = this.goods.giftId?0:this.goods.billTaxPrice || this.goods.taxPrice
        this.price.taxBoxPrice = this.goods.giftId?0:this.goods.billTaxBoxPrice || this.goods.taxBoxPrice
      }
      this.boxNum = null
      this.mainNum = null
      // this.goods.imageUrl = this.goodsImgUrl
      this.goods.isZeroPrice = 0
      this.isZeroPrice = false
      this.animation = animation
      this.goods.goodsTagId = this.oldGoods.goodsTagId
      this.goods.goodsTagName = this.oldGoods.goodsTagName
      this.showTagName = this.goods.goodsTagName
      this.price.taxAmount = 0
      this.price.taxBoxAmount = 0
          new Promise((resolve, reject) => {
        setTimeout(function() {
          that.fadeIn(); //调用显示动画
        }, 200)
      }).then(res=>{

      })
    },
    tagChange(e){
      console.log('触发1')
      this.list=[]
      const index = e.target.value
      this.showTagName = this.tagList[index].name;
      this.goods.goodsTagId = this.tagList[index].id
      this.goods.goodsTagName = this.tagList[index].name
      this.isZeroPrice = this.tagList[index].isZeroPrice == '1'
      this.goods.isZeroPrice = this.isZeroPrice
      if (this.isZeroPrice || this.goods.giftId){
        this.goods.taxPrice = 0
        this.price.taxPrice = 0
        this.goods.taxBoxPrice = 0
        this.goods.billTaxPrice = 0
        this.goods.billTaxBoxPrice = 0
        this.price.taxBoxPrice = 0
        this.price.taxAmount = 0
        this.price.taxBoxAmount = 0
      }else if (this.goods.isPromotion === 1 && this.goods.promotionType === '1' && !this.goods.isZeroPrice){
          if ((this.goods.goodsCount <= this.goods.cutOffGoodsCount && this.goods.goodsCount >= this.goods.effectGoodsCount) || this.goods.cutOffGoodsCount === 0 || (this.goods.cutOffGoodsCount > 0 && this.goods.goodsCount <= this.goods.cutOffGoodsCount)){
            this.goods.taxPrice = this.goods.promotionTaxPrice
            this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
            this.goods.taxBoxPrice = this.goods.promotionTaxBoxPrice
            this.price.taxBoxPrice = this.goods.newTaxBoxPrice || this.goods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
          }else {
            this.goods.taxPrice =  this.goods.newTaxPrice || this.oldGoods.taxPrice
            this.goods.taxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.taxBoxPrice
            this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
            this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
          }
        this.goods.billTaxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
        this.goods.billTaxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
        }else {
          this.goods.taxPrice =  this.goods.newTaxPrice || this.oldGoods.taxPrice
          this.goods.taxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.taxBoxPrice
          this.goods.billTaxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
          this.goods.billTaxBoxPrice = this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
          this.price.taxPrice = this.goods.newTaxPrice || this.oldGoods.billTaxPrice || this.oldGoods.taxPrice
          this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.oldGoods.billTaxBoxPrice || this.oldGoods.taxBoxPrice
        }
        // this.price.taxPrice = this.goods.newTaxPrice || this.goods.billTaxPrice || this.goods.taxPrice
        // this.price.taxBoxPrice =this.goods.newTaxBoxPrice || this.goods.billTaxBoxPrice || this.goods.taxBoxPrice
        this.goods.taxBoxAmount = this.goods.taxBoxPrice * Number(this.goods.boxNum)
        this.price.taxBoxAmount = this.goods.taxBoxAmount
        this.goods.taxAmount = Number(this.goods.mainNum) * Number(this.price.taxPrice)
        this.price.taxAmount = this.goods.taxAmount
      },
    setGoods(item){
      this.oldGoods = item
     this.goods = item
      let goodsImgUrl = ''
      try {
        goodsImgUrl = JSON.parse(this.goods.imageUrl)[0].path
      }catch (err){
        goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
      }
      this.oldGoods.imageUrl = goodsImgUrl
      this.goods.imageUrl = goodsImgUrl
      this.price.taxPrice = this.goods.giftId?0:this.goods.billTaxPrice || this.goods.taxPrice
      this.price.taxBoxPrice = this.goods.giftId?0:this.goods.billTaxBoxPrice || this.goods.taxBoxPrice
      this.boxNum = this.goods.boxNum
      this.mainNum = this.goods.mainNum
      this.isZeroPrice = this.goods.isZeroPrice
    },

    computedNum(){
      console.log('触发2')
      this.goods.mainNum = this.mainNum
      this.goods.boxNum = this.boxNum
      if (Number(this.mainNum) >= Number(this.goods.boxUnitCount)){
        const boxCount = parseInt(Number(this.goods.mainNum)/Number(this.goods.boxUnitCount))
        const mainCount = Number(this.goods.mainNum)%Number(this.goods.boxUnitCount)
        this.boxNum += boxCount
        this.goods.boxNum = this.boxNum
        this.mainNum = mainCount
        this.goods.mainNum = mainCount
      }
      if (this.goods.isPromotion === 1 && this.goods.promotionType === '1' && !this.goods.isZeroPrice){
        if ((this.goods.goodsCount <= this.goods.cutOffGoodsCount && this.goods.goodsCount >= this.goods.effectGoodsCount) || this.goods.cutOffGoodsCount === 0 || (this.goods.cutOffGoodsCount > 0 && this.goods.goodsCount <= this.goods.cutOffGoodsCount)){
          this.goods.taxPrice = this.goods.promotionTaxPrice
          this.price.taxPrice = this.goods.newTaxPrice || this.goods.billTaxPrice || this.goods.taxPrice
          this.goods.taxBoxPrice = this.goods.promotionTaxBoxPrice
          this.price.taxBoxPrice = this.goods.newTaxBoxPrice || this.goods.billTaxBoxPrice || this.goods.taxBoxPrice
        }
      }else{
        if (this.goods.isZeroPrice || this.goods.giftId){
          this.goods.taxPrice = 0
          this.price.taxPrice = 0
          this.goods.taxBoxPrice = 0
          this.price.taxBoxPrice = 0
        }else {
          this.goods.taxPrice = this.oldGoods.taxPrice
          this.price.taxPrice = this.goods.newTaxPrice || this.goods.billTaxPrice || this.goods.taxPrice
          this.goods.taxBoxPrice = this.oldGoods.taxBoxPrice
          this.price.taxBoxPrice = this.goods.newTaxBoxPrice || this.goods.billTaxBoxPrice || this.goods.taxBoxPrice
        }
      }
    },
    computedAmount(type){
      this.height = 600
      if (type === 'taxPrice'){
        this.goods.taxPrice = this.price.taxPrice
        this.goods.taxBoxPrice = Number(this.goods.taxPrice)*Number(this.goods.boxUnitCount)
        this.price.taxBoxPrice = this.goods.taxBoxPrice
        this.goods.taxBoxAmount = this.goods.taxBoxPrice * Number(this.goods.boxNum)
        this.price.taxBoxAmount = this.goods.taxBoxAmount
        this.goods.taxAmount = Number(this.goods.mainNum) * Number(this.goods.taxPrice)
        this.price.taxAmount = this.goods.taxAmount
        this.goods.billTaxPrice = this.goods.taxPrice
        this.goods.billTaxBoxPrice = this.goods.taxBoxPrice
        this.goods.newTaxPrice = this.goods.taxPrice
        this.goods.newTaxBoxPrice = this.goods.taxBoxPrice
        this.goods.isUpdate = true
      }else{
        this.goods.taxBoxPrice = this.price.taxBoxPrice
        this.goods.taxPrice = (Number(this.goods.taxBoxPrice)/Number(this.goods.boxUnitCount)).toFixed(2)
        this.price.taxPrice = this.goods.taxPrice
        this.goods.taxBoxAmount = this.goods.taxBoxPrice * Number(this.goods.boxNum)
        this.price.taxBoxAmount = this.goods.taxBoxAmount
        this.goods.taxAmount = Number(this.goods.mainNum) * Number(this.goods.taxPrice)
        this.price.taxAmount = this.goods.taxAmount
        this.goods.billTaxPrice = this.goods.taxPrice
        this.goods.billTaxBoxPrice = this.goods.taxBoxPrice
        this.goods.newTaxPrice = this.goods.taxPrice
        this.goods.newTaxBoxPrice = this.goods.taxBoxPrice
        this.goods.isUpdate = true
      }
    },
    async getPromotionList(promotionId){
      giftPromotionDetail({customerId:this.customerId,promotionId:promotionId}).catch(({content})=>{

      this.remark = content.promotionInfo.remark
      this.promotionName = content.promotionInfo.name
      this.ruleList = content.promotionInfo.detailList[0].ruleList
      const giftList = content.promotionInfo.detailList[0].giftList
      this.giftList = giftList.filter(item=>item.isGift === 1)
      this.promotionGoodsList = giftList.filter(item=>item.isGift === 0)
      for(let i in this.giftList){
        const goodsTagId = this.giftList[i].goodsTagId
        const goodsTagName = this.giftList[i].goodsTagName
        const isGiftPaperBox= this.giftList[i].isGiftPaperBox
        let goodsImgUrl
        const goods = content.goodsList.find(val=>this.giftList[i].goodsId === val.goodsId)
        try {
          if (goods.imageUrl){
            goodsImgUrl = JSON.parse(goods.imageUrl)[0].path
          }else {
            goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
          }
        }catch (e) {
          goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
        }

        this.$set(this.giftList,i,Object.assign({},goods,
            {boxNum: 0,mainNum: 0,goodsCount: 0,taxAmount:0,noTaxAmount:0,taxPrice:0,noTaxPrice:0,taxBoxPrice:0,noTaxBoxPrice:0,
              oldTaxPrice:0,
              oldNoTaxPrice:0,
              oldTaxBoxPrice:0,
              oldNoTaxBoxPrice:0,
              oldTaxAmount:0,
              oldNoTaxAmount:0,
              oldTax:0,
              acceptTaxAmount:0,
              acceptNoTaxAmount:0,
              acceptTax:0,
              billTaxPrice:0,
              billTaxBoxPrice:0,
              billTaxAmount:0,
              goodsTagId,
              goodsTagName,
              isGiftPaperBox,
              isZeroPrice:1,
              isGift: '1',
              promotionType:'3',
              isPromotion: 1,
              imageUrl: goodsImgUrl,
              isPromotionAdd:1,
              promotionId: this.promotionId,
              promotionCode: this.promotionCode,
              promotionName: this.promotionName,
            }
            )
        )

      }
      for(let i in this.promotionGoodsList){
        let goodsImgUrl
        const goods = content.goodsList.find(val=>this.promotionGoodsList[i].goodsId === val.goodsId)
        try {
          if (goods.imageUrl){
            goodsImgUrl = JSON.parse(goods.imageUrl)[0].path
          }else {
            goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
          }
        }catch (e) {
          goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
        }

        this.$set(this.promotionGoodsList,i,Object.assign({},goods,{boxNum: 0,mainNum: 0,goodsCount: 0,isZeroPrice:0,isPromotion: 1,promotionType:'3',imageUrl:goodsImgUrl}))
      }
      this.goods = this.promotionGoodsList.find(item=>item.goodsId === this.goods.goodsId)
      this.isZeroPrice = this.goods.isZeroPrice
      })
    },
    addPromotionList(){
        const promotionGoodsList = this.promotionGoodsList.filter(item => item.goodsCount > 0)
        //获取活动商品箱数
        let promotionBoxCount = 0
        //获取赠品数量
        let promotionCount = 0
        promotionGoodsList.forEach(item => {
          promotionCount += Number(item.goodsCount)
          promotionBoxCount += Number(item.boxNum)
        })
        const giftList = this.giftList.filter(item => item.goodsCount > 0)
        //获取赠品箱数
        let giftBoxCount = 0
        //获取赠品数量
        let giftCount = 0
        giftList.forEach(item => {
          giftCount += Number(item.goodsCount)
          giftBoxCount += Number(item.boxNum)
        })
        //判断活动商品满赠类型
        if (this.ruleList[0].reachType === 2) {
          //阶梯过滤出当前促销符合的条件
          const satisfyList = this.ruleList.filter(item => item.full <= promotionCount)
          //查询最大符合条件
          let satisfy = null
          satisfyList.forEach(item => {
            if (!satisfy || item.full > satisfy.full) {
              satisfy = item
            }
          })
          //符合促销
          if (satisfy) {
            //计算最大满足倍数
            const promotionNum = parseInt(promotionCount / satisfy.full)
            //计算最大赠送赠品数量
            const giftNum = promotionNum * satisfy.result
            //判断当前赠品数量是否超最大可赠送数量
            //判断赠品类型
            if (satisfy.resultType === 2) {
              if (giftCount > giftNum) {
                uni.showToast({title:`当前赠品数量大于可赠送数量，不允许下单;可赠数量为${giftNum}瓶，当前赠送数量为${giftCount}瓶`,icon:'none'})
              } else {
                  this.submitPromotionList()
                }

            } else if (satisfy.resultType === 3) {
              //判断当前赠品数量是否超最大可赠送数量
              const mainGift = this.giftList.find(item => item.mainNum > 0)
              if (mainGift) {
                uni.showToast({title:'赠送规格为箱,不允许输入零数',icon:'none'})
                return
              }
              if (giftBoxCount > giftNum) {
                  uni.showToast({title:`当前赠品数量大于可赠送数量，不允许下单;可赠数量为${giftNum}箱，当前赠送数量为${giftBoxCount}箱`,icon:'none'})
                } else {
                  this.submitPromotionList()
            }
          }else {
              const goodsList = this.promotionGoodsList.filter(item=>item.goodsCount > 0)
              goodsList.forEach(item=>{
                this.hideModal = false
                item.remark = item.giftRemark
                this.taxAmount = item.taxPrice * item.goodsCount
                item.joldTaxPrice = item.taxPrice
                item.joldTaxBoxPrice =item.taxBoxPrice
                this.$emit('addGoods',[Object.assign({},item),false])
              })
          }
        }}
        else  if (this.ruleList[0].reachType === 3) {
          //阶梯过滤出当前促销符合的条件
          const satisfyList = this.ruleList.filter(item => item.full <= promotionBoxCount)
          //查询最大符合条件
          let satisfy = null
          satisfyList.forEach(item => {
            if (!satisfy || item.full > satisfy.full) {
              satisfy = item
            }
          })
          //符合促销
          if (satisfy){
            //计算最大满足倍数
            const promotionNum = parseInt(promotionBoxCount / satisfy.full)
            //计算最大赠送赠品数量
            const giftNum = promotionNum * satisfy.result
            console.log(`活动商品箱数${promotionBoxCount},满促条件${ satisfy.full },单词满足可赠为${promotionNum},满足可赠箱数${giftNum}`)
            //判断当前赠品数量是否超最大可赠送数量
            //判断赠品类型
            if (satisfy.resultType === 2) {
              if (giftCount > giftNum) {
                uni.showToast({title:`当前赠品数量大于可赠送数量，不允许下单;可赠数量为${giftNum}瓶，当前赠送数量为${giftCount}瓶`,icon:'none'})
              } else {
                this.submitPromotionList()
              }
          } else if (satisfy.resultType === 3) {
              //判断当前赠品数量是否超最大可赠送数量
              const mainGift = this.giftList.find(item=>item.mainNum > 0)
              if (mainGift){
                uni.showToast({title:'赠送规格为箱,不允许输入零数',icon:'none'})
                return
              }
              if (giftBoxCount > giftNum) {
                uni.showToast({title:`当前赠品数量大于可赠送数量，不允许下单;可赠数量为${giftNum}箱，当前赠送数量为${giftBoxCount}箱`,icon:'none'})
              } else {
                this.submitPromotionList()
              }
            }
          }else {
            // uni.showToast({title: '当前活动商品数量不满足促销条件', icon: 'none'})
            const goodsList = this.promotionGoodsList.filter(item=>item.goodsCount > 0)
            goodsList.forEach(item=>{
              this.hideModal = false
              item.remark = item.giftRemark
              this.taxAmount = item.taxPrice * item.goodsCount
              item.joldTaxPrice = item.taxPrice
              item.joldTaxBoxPrice =item.taxBoxPrice
              this.$emit('addGoods',[Object.assign({},item),false])
            })
          }
      }
    },
   async submitPromotionList(){

      const date = new Date().getTime()
      const  giftList = this.giftList.filter(item => item.goodsCount > 0)
      const giftEmptyBoxList = giftList.filter(item=>item.packageType == 1 && item.isGiftPaperBox == 0)
      const goodsIds = giftEmptyBoxList.map(item=>item.goodsId).join(',')
     const promotionList = this.promotionGoodsList.filter(item => item.goodsCount > 0)
      // const emptyBoxList = []
      if (goodsIds){
        let content
        await paperGoodsWithEmptyBoxGoodsList({ids:goodsIds,customerId: this.customerId}).catch(res=>{
          content = res.content
        })
        for (let i of content){
          const gift = this.giftList.find(item=>item.goodsId === i.id)
          if (!gift){
            continue
          }
          let uuid
              // = uuidv4().split('-')
          uuid = new Date().getTime()
          const emptyBox = i.paperEmptyBoxGoodsInfo
          if (!emptyBox){
            continue
          }
          emptyBox.boxNum = Number(gift.boxNum)
          emptyBox.mainNum = 0
          emptyBox.goodsCount = emptyBox.boxNum * Number(emptyBox.boxUnitCount)
          emptyBox.taxAmount = emptyBox.goodsCount * emptyBox.taxPrice
          emptyBox.billTaxAmount = emptyBox.billTaxPrice * emptyBox.goodsCount
          emptyBox.boxCount = emptyBox.boxNum
          emptyBox.isEmptyBoxAdd = '1'
          emptyBox.isPromotionAdd = 1
          emptyBox.remark = this.remark
          gift.promotionEmptyBoxKey = uuid
          emptyBox.promotionEmptyBoxKey = uuid
          const index = this.giftList.indexOf(gift)
          giftList.splice(index + 1,0,emptyBox)
          // emptyBoxList.push(emptyBox)
        }
      }

      const promotionGoodsList = [...this.promotionGoodsList.filter(item => item.goodsCount > 0),...giftList]
      for (let i of promotionGoodsList){
        i.promotionGroupKey = date
        i.remark = this.remark
        i.isPromotion = 1
        i.promotionId = i.isEmptyBoxAdd?null:this.promotionId
        i.acceptBoxNum=i.boxNum
        i.isOne =  0
        i.acceptMainNum=i.mainNum
        i.promotionCode = i.isEmptyBoxAdd?null:this.promotionCode
        i.promotionName = i.isEmptyBoxAdd?null:this.promotionName
        i.sourceDetailType = i.isEmptyBoxAdd?'1':'2'
      }
     if (promotionList.length ===1 && giftList.filter(item=>item.isEmptyBoxAdd !== '1').length === 1){
       const promotionGoods = promotionGoodsList[0]

       promotionGoods.promotionData = {}
       promotionGoods.promotionData.gift = promotionGoodsList[1]
       promotionGoods.promotionData.ruleList = this.ruleList
       promotionGoods.isOne = 1
       promotionGoods.promotionData.emptyBox = promotionGoodsList[2]
       promotionGoods.promotionData = JSON.stringify(promotionList[0].promotionData)
     }
     // this.hideModal = false
      this.$emit('subPromotionList',promotionGoodsList)
     this.close()
    },
    getGiftCount(isGift){
      if (!this.promotionGoodsList){
        return
      }
      const promotionGoodsList = this.promotionGoodsList.filter(item => item.goodsCount > 0)
      //获取活动商品箱数
      let promotionBoxCount = 0
      //获取赠品数量
      let promotionCount = 0
      promotionGoodsList.forEach(item => {
        promotionCount += Number(item.goodsCount)
        promotionBoxCount += Number(item.boxNum)
      })
      const giftList = this.giftList.filter(item => item.goodsCount > 0)
      //获取赠品箱数
      let giftBoxCount = 0
      //获取赠品数量
      let giftCount = 0
      giftList.forEach(item => {
        giftCount += Number(item.goodsCount)
        giftBoxCount += Number(item.boxNum)
      })

      //判断活动商品满赠类型
      if (this.ruleList[0].reachType === 2) {
        //阶梯过滤出当前促销符合的条件
        const satisfyList = this.ruleList.filter(item => item.full <= promotionCount)
        //查询最大符合条件
        let satisfy = null
        satisfyList.forEach(item => {
          if (!satisfy || item.full > satisfy.full) {
            satisfy = item
          }
        })
        //符合促销
        if (satisfy) {
          //计算最大满足倍数
          const promotionNum = parseInt(promotionCount / satisfy.full)
          //计算最大赠送赠品数量
          let giftNum = promotionNum * satisfy.result
          //判断当前赠品数量是否超最大可赠送数量
          if (this.giftList.length === 1 && !isGift){
            if (satisfy.resultType === 2){
              this.giftList[0].goodsCount = giftNum
              const boxCount = parseInt(Number(this.giftList[0].goodsCount)/Number(this.giftList[0].boxUnitCount))
              this.giftList[0].mainNum = Number(this.giftList[0].goodsCount) % Number(this.giftList[0].goodsCount.boxUnitCount)
              this.giftList[0].boxNum = boxCount
              console.log(this.giftList[0])
            }else {
              this.giftList[0].goodsCount = giftNum * this.giftList[0].boxUnitCount
              this.giftList[0].mainNum = 0
              this.giftList[0].boxNum = giftNum
            }
            return `0${ satisfy.resultType === 2?'瓶':'箱' }`
          }
          return `${giftNum - ( satisfy.resultType === 2?giftCount:giftBoxCount )}${ satisfy.resultType === 2?'瓶':'箱' }`
        }else {
          return '0瓶'
        }
      }
      else  if (this.ruleList[0].reachType === 3) {
        //阶梯过滤出当前促销符合的条件
        const satisfyList = this.ruleList.filter(item => item.full <= promotionBoxCount)
        //查询最大符合条件
        let satisfy = null
        satisfyList.forEach(item => {
          if (!satisfy || item.full > satisfy.full) {
            satisfy = item
          }
        })
        //符合促销
        if (satisfy){
          //计算最大满足倍数
          const promotionNum = parseInt(promotionBoxCount / satisfy.full)
          //计算最大赠送赠品数量
          let giftNum = promotionNum * satisfy.result
          console.log(`活动商品箱数${promotionBoxCount},满促条件${ satisfy.full },单次满足可赠为${promotionNum},满足可赠箱数${giftNum}`)
          //判断当前赠品数量是否超最大可赠送数量
          //判断赠品类型
          if (this.giftList.length === 1 && !isGift){
            if (satisfy.resultType === 2){
              this.giftList[0].goodsCount = giftNum
              const boxCount = parseInt(Number(this.giftList[0].goodsCount)/Number(this.giftList[0].boxUnitCount))
              this.giftList[0].mainNum = Number(this.giftList[0].goodsCount) % Number(this.giftList[0].goodsCount.boxUnitCount)
              this.giftList[0].boxNum = boxCount
            }else {
              this.giftList[0].goodsCount = giftNum * this.giftList[0].boxUnitCount
              this.giftList[0].mainNum = 0
              this.giftList[0].boxNum = giftNum
              console.log(this.giftList[0])
            }
            return `0${ satisfy.resultType === 2?'瓶':'箱' }`
          }
          return `${giftNum - ( satisfy.resultType === 2?giftCount:giftBoxCount )}${ satisfy.resultType === 2?'瓶':'箱' }`
        }else {
          return '0瓶'
        }
      }
    },
    // 隐藏遮罩层
    close: function() {
      var that = this;
      var animation = wx.createAnimation({
        duration: 100, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
        timingFunction: 'ease', //动画的效果 默认值是linear
      })
      this.animation = animation
      that.fadeDown(); //调用隐藏动画
      setTimeout(function() {
        that.hideModal = false
        that.$emit('close',false)
      }, 120) //先执行下滑动画，再隐藏模块

    },
    onSubmitGoods(){
      if (!this.goods.boxNum && !this.goods.mainNum){
        uni.showToast({title:'请输入数量',icon:'error'})
        return
      }


      var that = this;
      var animation = wx.createAnimation({
        duration: 100, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
        timingFunction: 'ease', //动画的效果 默认值是linear
      })
      this.animation = animation
      that.fadeDown(); //调用隐藏动画
      setTimeout(function() {
            that.hideModal = false
        that.goods.remark = that.goods.giftRemark
        this.taxAmount = that.goods.taxPrice * that.goodsCount
        that.goods.joldTaxPrice = that.oldGoods.taxPrice
        that.goods.joldTaxBoxPrice = that.oldGoods.taxBoxPrice
        that.$emit('addGoods',[Object.assign({},that.goods),!that.show])
        console.log(that.goods)
      },120)
    },
    setGiftMargin(){
      this.giftTop = this.giftTop === 60?390:60
      this.retate = this.giftTop === 390?0:270
    },
    //动画集
    fadeIn: function() {
      this.animation.translateY(0).step()
      this.animationData = this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
    },
    fadeDown: function() {
      this.animation.translateY(335).step() //移动的距离
      this.animationData = this.animation.export()
    },
    addHeight(val){
      this.height = val
    },

    submit(){
      this.height = 600
      this.onSubmitGoods()
    }
  }
}
</script>

<style lang="scss" scoped>
.title{
  font-size: 14px;
  // color: #FFA220;
  height: 24px;
  line-height: 24px;
  padding-left: 8px;
}
.textDec{
  padding-left: 24px;
  color: #C8C9CC;
}
.dec{
  display: flex;
  justify-content: space-between;
  background-color: #E5E5E5;
  color: black;
  padding: 0 15px;
  line-height: 30px;
  height: 30px;
  text{
    display: inline-block;
  }
}

.container{
  .mask{
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0);
    z-index: 20;
    position: fixed;
    top: 0;
    left: 0;
  }
  .content{
    border-top: rgba(200,200,200,0.5) 1px solid;
    width: 100%;
    position: fixed;
    bottom: 0rpx;
    left: 0;
    background-color: #FFFFFF;
    z-index: 22;
    border-radius: 20rpx 20rpx 0 0;
    overflow: hidden;
  }
  /*动画前初始位置*/
  .bottom-pos {
    -webkit-transform: translateY(100%);
    transform: translateY(100%);
  }
}
.ipt{
  width: 150rpx;
  height: 80rpx;
  border-radius: 10rpx;
  border: rgba(200,200,200,0.5) solid 1px;
  text-align: center;
}
.ipt:hover{
  border: #527cde solid 1px;
}
image{
  width: 120rpx;
  height: 120rpx;
}
.mainBox{
  margin: 50rpx 0 0 50rpx;
}
.wih{
  height: 100rpx;
}
.picker{
  border: #07bb07 solid 1px;
  color: #07bb07;
  border-radius: 5rpx;
  padding: 5rpx;
}
.promotionList{
  height: 450rpx;
  position: fixed;
  bottom: 600rpx;
  width: 100%;
  background-color: #ffffff;
  z-index: 21;
  .title{
    height: 60rpx;
    padding: 10rpx;
    width: 100%;
    background-color: #c8c8f1;
    border: rgb(103,103,103) 1p solid;
  }
  .promotionGoodsList{
      height: 400rpx;
      z-index:1;
    background-color: #ffffff;
  }
  .giftList{
    height: 400rpx;
    z-index:2;
    position: absolute;
    width: 100%;
    transition: top 0.3s linear;
    background-color: #ffffff;
  }
}
.back{
  background-color: #ff9999;
}
.dropdownImg{
  width: 35rpx;
  height: 35rpx;
}
</style>
